package pl.skubicha.controller;

import javax.validation.Valid;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pl.skubicha.domain.CreditingForm;
import pl.skubicha.exceptions.CreditingFormNotFoundException;
import pl.skubicha.exceptions.EntityConstraintException;
import pl.skubicha.response.CreditingFormDTO;
import pl.skubicha.service.CreditingFormService;
import pl.skubicha.utils.CreditingFormMapper;

@Controller
@RequestMapping( { "/creditingform" } )
@PreAuthorize("hasAnyRole('Wykładowca')")
public class CreditingFormController {

	private static Logger logger = Logger.getLogger( CreditingFormController.class );

	@Autowired
	private CreditingFormService service;

	@RequestMapping( { "", "/" } )
	public String defaultPage( final ModelMap model) {

		model.addAttribute( "forms", CreditingFormMapper.map( service.findAll() ) );
		
		return "creditingform/list";
	}

	@RequestMapping( value = { "/create" }, method = RequestMethod.GET )
	public String create( final ModelMap model ) {
		model.addAttribute( "creditingFormDTO", new CreditingFormDTO() );
		model.addAttribute( "type", "create" );
		return "creditingform/edit";
	}

	@RequestMapping( value = { "/create" }, method = RequestMethod.POST )
	public String create( @Valid final CreditingFormDTO creditingFormDTO, BindingResult bindingResult, final ModelMap model ) {
		
		if ( bindingResult.hasErrors() ) {
			model.addAttribute( "type", "create" );
			return "creditingform/edit";
		}
		
		try {
			service.create( creditingFormDTO );
			model.addAttribute( "status", "form.create.success" );
		} catch ( final Exception ex ) {
			logger.debug( ex );
			model.addAttribute( "status", "form.create.failure" );
		}
		defaultPage(model);

		return "creditingform/list";
	}

	@RequestMapping( value = { "/edit/{id}" }, method = RequestMethod.GET )
	public String edit( @PathVariable( value = "id" ) final Integer id, final ModelMap model ) {

		try {
			final CreditingForm form = service.findById( id );
			model.addAttribute( "creditingFormDTO", CreditingFormMapper.map( form ) );
			model.addAttribute( "type", "edit" );
		} catch ( final CreditingFormNotFoundException ex ) {
			logger.debug( ex );
			model.addAttribute( "status", "form.not.found" );
			return "creditingform/list";
		}

		return "creditingform/edit";
	}

	@RequestMapping( value = { "/edit" }, method = RequestMethod.POST )
	public String update( @Valid final CreditingFormDTO creditingFormDTO, BindingResult bindingResult, final ModelMap model ) {
		
		if ( bindingResult.hasErrors() ) {
			model.addAttribute( "type", "edit" );
			return "creditingform/edit";
		}
		
		try {
			service.update( creditingFormDTO );
			model.addAttribute( "status", "form.update.success" );
		} catch ( final Exception ex ) {
			logger.debug( ex );
			model.addAttribute( "status", "form.update.failure" );
		}
		defaultPage(model);

		return "creditingform/list";
	}

	@RequestMapping( value = "/delete/{id}" )
	public String delete( @PathVariable( value = "id" ) final Integer id, final ModelMap model ) {
		try {
			service.delete( id );
			model.addAttribute( "status", "form.delete.success" );
		} catch ( final CreditingFormNotFoundException ex ) {
			model.addAttribute( "status", "form.not.found" );
			logger.debug( ex );
		} catch ( final EntityConstraintException ex ) {
			model.addAttribute( "status", "form.constraint.exception" );
			logger.debug( ex );
		}
		defaultPage(model);

		return "creditingform/list";
	}
}